def plot_sap_data(turbines):
    import sap_data
    filepath = ['/home/andbo/work/data_software/code/gui/sap-data-adjustment-rdspp-step-3.csv']
    clean_data, data_dict = sap_data.get_data_from_csv(filepath)
    start_date = dt.datetime(2010,2,1)
    stop_date = dt.datetime(2010,6,1)
    time_range = (start_date, stop_date)
    data = sap_data.find_specific_data(data_dict, turbines, time_range)
    turbines = [turb.lower() for turb in turbines]
    for i, turb in enumerate(turbines):
        for j, tid in enumerate(data["time"]):
            if turb == data["turbine"][j]:
                if (data["rdspp"][j] == "MDK20TL001") or (data["rdspp"][j] == "MDK20"):
                    plt.text(tid, i, "Gear", horizontalalignment = 'center', rotation=30, verticalalignment = 'center')
                #plt.text(tid, i, data["rdspp"][j], horizontalalignment = 'center', rotation=30, verticalalignment = 'center')
                #plt.text(tid, i, "S", horizontalalignment = 'center', rotation=30, verticalalignment = 'center')
    return




def generate_residual(data_sets, pls_outs, turbines, para_set, start_res_day,
        num_of_res_days=dt.timedelta(days=15)):
    x_parameters = para_set['x_para']
    y_parameters = para_set['y_para']
    stop_res_day = start_res_day + num_of_res_days
    res_dict = {}
    for i, turbine in enumerate(turbines):
        res_dict2 = {}
        for j, data in enumerate(data_sets):
            res_data = data[date_idx(data, date2num(start_res_day), date2num(stop_res_day))]
            xres = get_specific_data(res_data, x_parameters)
            yres = get_specific_data(res_data, y_parameters)
            res_dict2[turbines[j] + '-time'] = res_data['time']
            ypred = predict_pls(xres, pls_outs[i]['b'], pls_outs[i]['xmean'], pls_outs[i]['ymean'])
            res = yres - ypred
            res_dict2[turbines[j] + '-Residual'] = res
            res_dict2[turbines[j] + '-Yreal'] = yres
            res_dict2[turbines[j] + '-Ypred'] = ypred
        res_dict[turbine] = res_dict2
    return res_dict



def residual_evaluation(pls_outs, res_dict, turbines, threshold):
    """
    for pls_out in pls_outs:
        pls_out['vari'] = np.var(pls_out['predictions'] - pls_out['ymean'])
    """


    for i, turbine in enumerate(turbines):
        res = res_dict[turbine]
        #vari = pls_outs[i]['vari']
        res1 = res[turbine + '-Residual']
        mean = np.mean(res1)
        var = np.var(res1)
        std = np.std(res1)
        my0 = mean
        my1 = std * 6
        for turb in turbines:
            #print turb,
            z = res[turb + '-Residual']
            #print np.mean(z),
            #print np.var(z)
            cusum = (my1 - my0) / (var) * (z - (my0 + my1) / (2))
            res[turb + '-Cusum'] = cusum
            cumsum = np.cumsum(cusum)
            lastval = 0
            mindst = []

            for val in cumsum:
                mindst.append(min(val, lastval))
                lastval = mindst[-1]
            gumsum = cumsum - mindst
            res[turb + '-Gusum'] = gumsum

            bin_gu = []
            for gu in gumsum:
                if gu >= threshold:
                    bin_gu.append(1)
                else:
                    bin_gu.append(0)
            res[turb + '-Faultbin'] = bin_gu
    return res_dict

def residual_voting(res_dict, turbines, turbines_voting):
    """ Combining the votes from the voting turbines to have the total number of votes
    """
    voting_dict = {}
    for turbine in turbines:
        for i, turb in enumerate(turbines_voting):
            if i == 0:
                vote = res_dict[turb][turbine + '-Faultbin']
                tid = res_dict[turb][turbine + '-time']
            else:
                vote = np.add(vote, res_dict[turb][turbine + '-Faultbin'])
        voting_dict[turbine + '-Vote'] = vote
        voting_dict[turbine + '-time'] = tid
        if len(vote) > 0:
            if max(vote) > 0.5 * len(turbines_voting):
                voting_dict[turbine + '-Faulty'] = 1
            else:
                voting_dict[turbine + '-Faulty'] = 0
        else:
            voting_dict[turbine + '-Faulty'] = 0
    return voting_dict

def find_voting_turbines(voting_dict, turbines_voting):
    voting_turbs = []
    keep_looking = False
    for turb in turbines_voting:
        # if a voter is faulty look aging
        if voting_dict[turb + '-Faulty'] == 2:
            keep_looking = True
        else:
            voting_turbs.append(turb)
    return voting_turbs, keep_looking

def combine_votes(voting_dicts):
    """ This functions combing a list of voting dicts into one series of votes
    if time stamps is over lapping the oldest vote will be used.
    """
    combined_voting_dict = defaultdict(lambda : defaultdict(list))
    for tid, voting_dict in sorted(voting_dicts.iteritems()):
        for key, val in sorted(voting_dict.iteritems()):
            turbine = key.split('-')[0]
            meas_type = key.split('-')[1]
            if meas_type == "Faulty":
                continue
            combined_voting_dict[turbine][meas_type].extend(val)

    new_voting_dict = defaultdict(lambda : defaultdict(list))
    for turbine, voting_dict in combined_voting_dict.iteritems():
        old_time = voting_dict["time"][0]
        for i, tid in enumerate(voting_dict["time"]):
            if tid >= old_time:
                vote = voting_dict["Vote"][i]
                new_voting_dict[turbine]["time"].append(tid)
                new_voting_dict[turbine]["Vote"].append(vote)
            old_time = tid

def get_red_color(i):
    from matplotlib.colors import rgb2hex
    color_string = "255, 245, 240; 254, 224, 210; 252, 187, 161; 252, 146, 114; 251, 106, 74; 239, 59, 44; 203, 24, 29; 165, 15, 21; 103, 0, 13;"
    colors = color_string.split(";")[0:-1]
    if i >= len(colors):
        i = len(colors) - 1
    rgb_color = [float(color) / 255 for color in colors[i].split(",")]
    return rgb2hex(rgb_color)

def evaluate_votes(voting_dicts):
    combined_voting_dict = defaultdict(lambda : defaultdict(list))
    for tid, voting_dict in sorted(voting_dicts.iteritems()):
        for key, val in sorted(voting_dict.iteritems()):
            turbine = key.split('-')[0]
            meas_type = key.split('-')[1]
            if meas_type == "Faulty":
                combined_voting_dict[turbine][meas_type].extend([val for i in combined_voting_dict[turbine]["Vote"]])
                continue
            combined_voting_dict[turbine][meas_type].extend(val)

    #num_of_turbines = len(combined_voting_dict)
    turbines = []
    fig, axs = plt.subplots(1,1)
    for turbine, vote_dict in sorted(combined_voting_dict.iteritems()):
        turbines.append(turbine)
        old_time = vote_dict["time"][0]
        next_time = date2num(num2date(old_time) + dt.timedelta(days=5))
        for i, tid in enumerate(vote_dict["time"]):
            if tid >= next_time:
                next_time = date2num(num2date(tid) + dt.timedelta(days=5))
                vote = vote_dict["Vote"][i]
                color = get_red_color(vote)
                axs.plot(num2date(tid), len(turbines) - 1, color=color, ms=12, marker='o')
            else:
                pass

    axs.set_yticks(range(len(turbines)))
    axs.set_yticklabels(range(1, len(turbines) + 1))
    plt.ylabel('Turbine')
    axs.set_ymargin(0.5)
    axs.set_ylim((-0.5, len(turbines) - 0.5))
    axs.spines['top'].set_visible(False)
    axs.spines['right'].set_visible(False)
    axs.get_xaxis().tick_bottom()
    axs.get_yaxis().tick_left()
    fig.autofmt_xdate()
    return

